<?php 

class ConfigModel extends Model{
	public $table='config';

	//更新配置项
	public function updateConfig()
	{
		foreach ($_POST as $k => $v) {
			$data['value']=$v;
			if (!$this->where(" name='$k'")->save($data)) {
				$this->error="修改失败";
				return false;
			}	
		}
		//更新配置文件base.php (创建一个配置文件和之前的合并)
		return $this->createFile();
	}
	//更新配置文件base.php
	public function createFile()
	{
		$config=$this->getField('name,value');//自动把name做键名，value做键值 getField一个参数
		// 创建文件
		$data="<?php \n return ".var_export($config,true).";\n?>";
		return file_put_contents(APP_CONFIG_PATH.'base.php',$data);
	}
	//设置配置项
	public function setTplStyle()
	{
		$dir=Q('dir');
		$status=M('config')->where(" name='WEBSTYLE' ")->save(array('value'=>$dir));////
		return $this->createFile();
	}

	//获得配置项
	public function get()
	{
		$config=$this->all();
		foreach ($config as $k => $v) {
			$func='_'.$v['type'];
			$config[$k]['_html']=$this->$func($v);
		}
		return $config;
	}
	public function _text($v)
	{
		return "<input type='text' value='{$v['value']}' class='w200' name='{$v['name']}'>";
	}
	public function _radio($v)
	{
		$radio=explode(',', $v['info']);
		$html='';
		foreach ($radio as $k => $value) {
			$info=explode('|', $value);	
			$checked=$info[0]==$v['value']?" checked=''":'';
			$html.="<label><input type='radio' value='{$info[0]}' $checked name='{$v['name']}'>{$info[1]}</label>";
		}
		return $html;
	}
	public function _textarea($v)
	{
		return "<textarea name='{$v['name']}' class='w400 h100'>{$v['value']}</textarea>";
	}

}

 ?>